package leetcode_数组.二维数组及滚动数组;

// 甲板上的战舰
public class _419 {
    public static void main(String[] args) {
        char[][] board = new char[][]{{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}};
        System.out.println(countBattleships(board));
    }

    public static int countBattleships(char[][] board) {
        int m = board.length, n = board[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'X') {
                    boolean tem = true;
                    if (i - 1 >= 0 && dp[i - 1][j] == 1) {
                        tem = false;
                    }
                    if (j - 1 >= 0 && dp[i][j - 1] == 1) {
                        tem = false;
                    }
                    dp[i][j] = tem ? 1 : 0;
                    continue;
                }
                dp[i][j] = 0;
            }
        }
        int ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (dp[i][j] == 1) {
                    ans++;
                }
            }
        }
        return ans;
    }

}
